import 'package:dart_date/dart_date.dart';
import 'package:yt_devops/utils/constants/time_formated.dart';
import 'package:yt_devops/utils/func_utils.dart';

/**
 *
 * @Description TODO
 * @Author liuer
 * @CreateTime 2024年03月11日 15:20
 */
class ChartXDate {
  /// 获取时间轴的数据 年月日时分
  static List<String> getData({int timeInterval = 1} // 默认时间间隔为1分钟,间隔多久显示时间
      ) {
    List<String> dateList = [];
    // 设置起始时间为当天的00:00
    DateTime startTime = DateTime(
        DateTime.now().year, DateTime.now().month, DateTime.now().day, 0, 0);
    // 设置结束时间为当天的23:59
    DateTime endTime = DateTime(
        DateTime.now().year, DateTime.now().month, DateTime.now().day, 23, 59);
    // 使用while循环从起始时间遍历到结束时间
    DateTime currentTime = startTime;
    while (currentTime.isBefore(endTime) ||
        currentTime.isAtSameMomentAs(endTime)) {
      // 打印当前时间的小时和分钟
      String month = currentTime.month.toString();
      String day = currentTime.day.toString();
      String hour = currentTime.hour.toString();
      String minute = currentTime.minute.toString();
      if (currentTime.month < 10) {
        month = '0${currentTime.month}';
      }
      if (currentTime.day < 10) {
        day = '0${currentTime.day}';
      }
      if (currentTime.hour < 10) {
        hour = '0${currentTime.hour}';
      }
      if (currentTime.minute < 10) {
        minute = '0${currentTime.minute}';
      }
      dateList.add("${currentTime.year}-$month-$day $hour:$minute");
      // 将当前时间增加一分钟
      currentTime = currentTime.add(Duration(minutes: timeInterval));
    }
    return dateList;
  }

  ///获取一年12个月x轴坐标
  static List<String> getYearData() {
    List<String> dateList = [];
    List<int> hoursArray = List.generate(12, (index) => index + 1);
    for (var element in hoursArray) {
      String hourString = element.toString();
      if (element < 10) {
        hourString = '0$hourString';
      }
      dateList.add(hourString);
    }
    return dateList;
  }

  ///获取一个月内x轴坐标
  static List<String> getMonthData() {
    final now = DateTime.now();
    final daysInMonth = DateTime(now.year, now.month + 1, 0).day;
    List<String> dateList = [];
    List<int> hoursArray = List.generate(daysInMonth, (index) => index + 1);
    for (var element in hoursArray) {
      String hourString = element.toString();
      if (element < 10) {
        hourString = '0$hourString';
      }
      dateList.add(hourString);
    }
    return dateList;
  }

  ///获取一天24的x轴坐标
  static List<String> getHoursData() {
    List<String> dateList = [];
    List<int> hoursArray = List.generate(24, (index) => index);
    for (var element in hoursArray) {
      String hourString = element.toString();
      if (element < 10) {
        hourString = '0$hourString';
      }
      dateList.add('$hourString:00');
    }
    return dateList;
  }

  ///获取一年12个月x轴坐标
  static List<String> getTotalData() {
    return [DateTime.now().year.toString()];
  }

  static getDataByDuration(
      {required Duration duration,
      required List<String> initList,
      required DateTime startTime,
      required DateTime endTime}) {
    DateTime date;
    int times;
    if (!FuncUtils.isListEmpty(initList)) {
      date = DateTime.tryParse(initList.last) ?? endTime;
      if (initList.length > 1) {
        duration = Duration(
            milliseconds:
                DateTime.tryParse(initList[1])!.millisecondsSinceEpoch -
                    DateTime.tryParse(initList[0])!.millisecondsSinceEpoch);
      }
    } else {
      date = startTime;
      initList.add(date.format(TimeFormated.dateTimeFromated));
    }
    times =
        (endTime.getMillisecondsSinceEpoch - date.getMillisecondsSinceEpoch) ~/
            duration.inMilliseconds;

    while (times > 0) {
      date = date.add(duration);

      initList.add(date.format(TimeFormated.dateTimeFromated));

      times = times - 1;
    }
    return initList;
  }
}
